package com.jsnkj.basic.activity

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.jsnkj.basic.R
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.databinding.ActivityCustomerServiceBinding
import com.jsnkj.basic.dialog.JoinPlayerDialog
import com.jsnkj.basic.dialog.KFDialog
import com.jsnkj.basic.mvvm.BaseKtActivity
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.util.MediaUtil
import com.jsnkj.basic.util.SharePreferencesUtil

//联系客服
class CustomerServiceActivity : BaseKtActivity<ActivityCustomerServiceBinding, HttpViewModel>(),
    View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_customer_service

    var joinPlayerDialog: JoinPlayerDialog? = null

    var kfDialog: KFDialog? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        mVDB.topTitle.txtTopTitle.text = "联系客服"
        mVDB.topTitle.imageReturn.setOnClickListener {
            onBackPressed()
        }
        mVDB.topTitle.layoutTopTitle.setBackgroundColor(Color.parseColor("#FFFFFF"))
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.txt_Join_player -> {

                var content = SharePreferencesUtil.getInstance().getSettingEntity().player_group_url
                if (content.isNullOrEmpty()) {
                    com.jsnkj.basic.util.ToastUtils.show("数据获取失败，请稍后再试")
                    return
                }
                if (joinPlayerDialog?.isShowing == true) {
                    joinPlayerDialog?.dismiss()
                }

                joinPlayerDialog = JoinPlayerDialog(this@CustomerServiceActivity, content,
                    object : JoinPlayerDialog.onItemClickListener {
                        override fun saveItemClick(bitmap: Bitmap?) {
                            if (null == bitmap) {
                                com.jsnkj.basic.util.ToastUtils.show("保存失败")
                                return
                            }
                            MediaUtil.savePuzzleBitmap(
                                this@CustomerServiceActivity, bitmap, SpConfig.IMG_SAVE_NAME
                            ) { state, _ ->
                                if (state) {
                                    bitmap.recycle()
                                    com.jsnkj.basic.util.ToastUtils.show("已成功保存到相册")
                                } else {
                                    com.jsnkj.basic.util.ToastUtils.show("保存失败，请重试")
                                }
                            }
                        }
                    })

                joinPlayerDialog?.show()
            }

            R.id.txt_customer_service -> {
                gotoCustomerService()
            }

            R.id.txt_error -> {
                val content = SharePreferencesUtil.getInstance().getSettingEntity().service_url
                if (content.isNullOrEmpty()) {
                    com.jsnkj.basic.util.ToastUtils.show("数据获取失败，请稍后再试")
                    return
                }

                if (kfDialog?.isShowing == true) {
                    kfDialog?.dismiss()
                }

                kfDialog = KFDialog(this, content,
                    object : KFDialog.onItemClickListener {
                        override fun saveItemClick(bitmap: Bitmap?) {
                            if (null == bitmap) {
                                com.jsnkj.basic.util.ToastUtils.show("保存失败")
                                return
                            }
                            MediaUtil.savePuzzleBitmap(
                                this@CustomerServiceActivity, bitmap, SpConfig.IMG_SAVE_NAME
                            ) { state, _ ->
                                if (state) {
                                    bitmap.recycle()
                                    com.jsnkj.basic.util.ToastUtils.show("已成功保存到相册")
                                } else {
                                    com.jsnkj.basic.util.ToastUtils.show("保存失败，请重试")
                                }
                            }
                        }
                    })
                kfDialog?.show()
            }
        }
    }


}